package com.burpee.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.burpee.ui.GroupMember;
import com.burpee.ui.Message;

@Repository(value="messageDao")
public class MessageDaoImpl implements MessageDao {

	SqlSessionTemplate sqlSessionTemplate;

	@Autowired
	public void setSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
	}

	public List<Message> list(HashMap<String, String> listdata) {
		List<Message> list = null;

		try {
			list = sqlSessionTemplate.selectList("messagelist",listdata);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public Message view(long messageNo) {
		Message messageview = new Message();

		try {
			messageview = sqlSessionTemplate.selectOne("messageview", messageNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return messageview;
	}

	public int add(Message message) {
		int result = 0;
		if(message.getMessageChoiceaccept() == null)
		{
			try {
				result = (int)sqlSessionTemplate.insert("messageadd",message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return result;
		} else {
			
			try {
				result = (int)sqlSessionTemplate.insert("invitemessageadd",message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return result;
		}

	}

	public int remove(Message message) {
		int result = 0;

		try {
			result = (int)sqlSessionTemplate.update("messageremove",message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	public int countmessage(String memberId) {
		int result = 0;
		
		try {
			result= (Integer)sqlSessionTemplate.selectOne("countmessage",memberId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		
		return result;
	}
}
